package xju.rjxy.shop.service.impl;

import xju.rjxy.shop.constant.BaseConstant;
import xju.rjxy.shop.mapper.FriendMapper;
import xju.rjxy.shop.mapper.impl.FriendMapperImpl;
import xju.rjxy.shop.model.domain.Friend;
import xju.rjxy.shop.model.vo.FriendVO;
import xju.rjxy.shop.service.IFriendService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 好友服务接口实现类
 */
public class FriendServiceImpl implements IFriendService {
    private final FriendMapper friendMapper = new FriendMapperImpl();

    /**
     * 获取好友列表
     */
    @Override
    public List<FriendVO> getFriendList() {
        List<FriendVO> friendList = friendMapper.selectFriendList();
        if (friendList != null && friendList.size() > 0) {
            friendList.forEach(friend -> {
                // 处理状态文本值
                friend.setFlagValue(getFlagValue(friend.getFlag()));
            });
        }
        return friendList;
    }
    
    /**
     * 根据用户ID获取好友列表
     */
    @Override
    public List<FriendVO> getFriendListByUserId(Integer userId) {
        if (userId == null) {
            throw new RuntimeException("用户ID不能为空");
        }
        
        List<FriendVO> friendList = friendMapper.selectByUserId(userId);
        processFriendList(friendList);
        return friendList;
    }
    
    /**
     * 根据分组获取好友列表
     */
    @Override
    public List<FriendVO> getFriendListByTag(Integer userId, String tag) {
        if (userId == null) {
            throw new RuntimeException("用户ID不能为空");
        }
        if (tag == null || tag.trim().isEmpty()) {
            throw new RuntimeException("分组名称不能为空");
        }
        
        List<FriendVO> friendList = friendMapper.selectByTag(userId, tag);
        processFriendList(friendList);
        return friendList;
    }
    
    /**
     * 删除好友
     */
    @Override
    public boolean deleteFriend(Integer userId, Integer friendId) {
        if (userId == null || friendId == null) {
            throw new RuntimeException("用户ID和好友ID不能为空");
        }
        
        int result = friendMapper.deleteFriend(userId, friendId);
        return result > 0;
    }
    
    /**
     * 添加好友
     */
    @Override
    public boolean addFriend(Friend friend) {
        if (friend == null || friend.getUserId() == null || friend.getFriendId() == null) {
            throw new RuntimeException("好友信息不完整");
        }
        System.out.println("[Service.addFriend] 入参: " + friend);
        // 设置默认值
        friend.setCreateTime(LocalDateTime.now());
        friend.setFlag(1); // 待确认状态
        friend.setStatusCode("PENDING");
        System.out.println("[Service.addFriend] 设置默认值后: " + friend);
        int result = friendMapper.insertFriend(friend);
        System.out.println("[Service.addFriend] Mapper返回: " + result);
        return result > 0;
    }
    
    /**
     * 更新好友分组
     */
    @Override
    public boolean updateFriendTag(Integer userId, Integer friendId, String tag) {
        if (userId == null || friendId == null) {
            throw new RuntimeException("用户ID和好友ID不能为空");
        }
        if (tag == null || tag.trim().isEmpty()) {
            throw new RuntimeException("分组名称不能为空");
        }
        
        int result = friendMapper.updateFriendTag(userId, friendId, tag);
        return result > 0;
    }

    @Override
    public boolean acceptFriend(Integer userId, Integer friendId) {
        // userId为被申请人，friendId为申请人
        int result = friendMapper.updateFriendStatus(friendId, userId, 2, "ACCEPTED");
        return result > 0;
    }

    @Override
    public boolean rejectFriend(Integer userId, Integer friendId) {
        // userId为被申请人，friendId为申请人
        int result = friendMapper.updateFriendStatus(friendId, userId, 3, "REJECTED");
        return result > 0;
    }

    @Override
    public List<FriendVO> getPendingRequestList(Integer userId) {
        return friendMapper.selectPendingRequestList(userId);
    }

    @Override
    public List<FriendVO> getAllFriendsWithPending(Integer userId) {
        return friendMapper.selectAllFriendsWithPending(userId);
    }

    @Override
    public List<FriendVO> getAllFriendsWithPendingPage(Integer userId, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<FriendVO> list = friendMapper.selectAllFriendsWithPendingPage(userId, offset, pageSize);
        processFriendList(list);
        return list;
    }

    @Override
    public int getAllFriendsWithPendingTotal(Integer userId) {
        return friendMapper.selectAllFriendsWithPendingTotal(userId);
    }

    private String getFlagValue(Integer flag) {
        if (flag == null) {
            return BaseConstant.EMPTY;
        }

        // hard code
        switch (flag) {
            case 1:
                return "待确认";
            case 2:
                return "已同意";
            case 3:
                return "已拒绝";
            case 4:
                return "已删除";
            default:
                return BaseConstant.EMPTY;
        }
    }
    
    /**
     * 处理好友列表，设置状态文本值
     */
    private void processFriendList(List<FriendVO> friendList) {
        if (friendList != null && friendList.size() > 0) {
            friendList.forEach(friend -> {
                friend.setFlagValue(getFlagValue(friend.getFlag()));
            });
        }
    }

    /**
     * 获取单个好友详情
     */
    @Override
    public FriendVO getFriendInfo(Integer userId, Integer friendId) {
        // 可加权限校验
        return friendMapper.selectFriendInfo(userId, friendId);
    }
} 